package cc.yugu.yhdw.common.domain.hdw;

import cc.yugu.yhdw.common.annotation.Transient;
import cc.yugu.yhdw.common.domain.PageEntity;
import cc.yugu.yhdw.common.entity.json.DateTimeSerializer;
import org.apache.commons.lang.StringUtils;
import org.codehaus.jackson.map.annotate.JsonSerialize;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class UnregisterBatteryReportLog extends PageEntity {

    public final static String UNREGISTER_BATTERY_REPORT_LOG_TABLE_NAME = "hdw_unregister_battery_report_log_";

    protected String batteryId; //电池ID
    protected String protectionStatus; //保护状态
    protected String ntcTemperature; //温度
    protected Integer batteryVolume; //电池电量(SOC)
    protected Double lng; //经度
    protected Double lat; //纬度
    protected Double speed; //速度
    protected Double electricity; //上传协议间隔之间骑行所消耗的电量
    protected Integer distance; //上传协议间隔之间骑行的公里数
    protected Integer dumpEnergy; //剩余电量
    protected Integer reportInterval; //上报间隔
    protected Date createTime; //创建时间

    @Transient
    protected Date queryLogTime;
    protected String suffix;

    /**
     * 新增电池保护状态转换
     * @Author zxj 2018/7/23
     */
    public enum ProtectionStatus {
        CODE_1(1, "单体过压发生保护"),
        CODE_2(2, "单体欠压发生保护"),
        CODE_3(3, "整组过压发生保护"),
        CODE_4(4, "整组欠压发生保护"),
        CODE_5(5, "充电过温发生保护"),
        CODE_6(6, "充电低温发生保护"),
        CODE_7(7, "放电过温发生保护"),
        CODE_8(8, "放电低温发生保护"),
        CODE_9(9, "充电过流发生保护"),
        CODE_10(10, "放电过流发生保护"),
        CODE_11(11, "短路发生保护"),
        CODE_12(12, "前端检测IC错误"),
        CODE_13(13, "软件锁定MOS"),
        CODE_14(14, ""),
        CODE_15(15, ""),
        CODE_16(16, ""),;

        private final int value;
        private final String name;

        ProtectionStatus(int value, String name) {
            this.value = value;
            this.name = name;
        }

        private static Map<Integer, String> map = new HashMap<Integer, String>();

        static {
            for (ProtectionStatus e : ProtectionStatus.values()) {
                map.put(e.getValue(), e.getName());
            }
        }

        public static String getName(int value) {
            return map.get(value);
        }

        public int getValue() {
            return value;
        }

        public String getName() {
            return name;
        }
    }

    public UnregisterBatteryReportLog() {
    }

    public UnregisterBatteryReportLog(String batteryId, String protectionStatus, String ntcTemperature, String batteryVolume, Double lng, Double lat,
                                      String speed, String electricity, String distance, String dumpEnergy, String reportInterval, Date createTime) {
        this.batteryId = batteryId;
        this.protectionStatus = protectionStatus;
        this.ntcTemperature = ntcTemperature;
        if (StringUtils.isNotEmpty(batteryVolume)) {
            this.batteryVolume = Integer.parseInt(batteryVolume);
        }
        if (StringUtils.isNotEmpty(speed)) {
            this.speed = Double.parseDouble(speed);
        }
        if (StringUtils.isNotEmpty(reportInterval)) {
            this.reportInterval = Integer.parseInt(reportInterval);
        }
        if (StringUtils.isNotEmpty(distance)) {
            this.distance = Integer.valueOf(distance);
        }
        if (StringUtils.isNotEmpty(electricity)) {
            this.electricity = Double.parseDouble(electricity);
        }
        if (StringUtils.isNotEmpty(dumpEnergy)) {
            this.dumpEnergy = Integer.valueOf(dumpEnergy);
        }
        this.lng = lng;
        this.lat = lat;
        this.createTime = createTime;
    }

    public UnregisterBatteryReportLog(String protectionStatus, String ntcTemperature, String batteryVolume, Double lng, Double lat,
                                      String speed, String electricity, String distance, String dumpEnergy, String reportInterval, Date createTime) {
        //this.protectionStatus = protectionStatus;
        // 添加未注册电池保护状态处理--16进制转换为10进制
        if (StringUtils.isNotEmpty(protectionStatus)) {
           this.protectionStatus = Integer.valueOf(protectionStatus, 16).toString();
        }
        //this.ntcTemperature = ntcTemperature;
        // 添加未注册电池温度处理
        if (StringUtils.isNotEmpty(ntcTemperature)) {
            String[] list = StringUtils.split(ntcTemperature, ", ");
            String t = null;
            for (String str : list) {
                if (t == null) {
                    t = String.valueOf((Integer.parseInt(str, 16) - 2731) / 10);
                } else {
                    t += "," + (Integer.parseInt(str, 16) - 2731) / 10;
                }
            }
            this.ntcTemperature = t;
        }
        if (StringUtils.isNotEmpty(batteryVolume)) {
            this.batteryVolume = Integer.parseInt(batteryVolume, 16);
        }
        if (StringUtils.isNotEmpty(speed)) {
            this.speed = Double.parseDouble(speed);
        }
        if (StringUtils.isNotEmpty(reportInterval)) {
            this.reportInterval = Integer.valueOf(reportInterval);
        }
        if (StringUtils.isNotEmpty(distance)) {
            this.distance = Integer.parseInt(distance, 16);
        }
        if (StringUtils.isNotEmpty(electricity)) {
            this.electricity = Double.valueOf(Integer.parseInt(electricity, 16));
        }
        if (StringUtils.isNotEmpty(dumpEnergy)) {
            this.dumpEnergy = Integer.parseInt(dumpEnergy, 16);
        }

        this.lng = lng;
        this.lat = lat;
        this.createTime = createTime;
    }

    @JsonSerialize(using = DateTimeSerializer.class)
    public Date getQueryLogTime() {
        return queryLogTime;
    }

    public void setQueryLogTime(Date queryLogTime) {
        this.queryLogTime = queryLogTime;
    }

    public String getSuffix() {
        return suffix;
    }

    public void setSuffix(String suffix) {
        this.suffix = suffix;
    }

    public String getBatteryId() {
        return batteryId;
    }

    public void setBatteryId(String batteryId) {
        this.batteryId = batteryId;
    }

    public String getProtectionStatus() {
        return protectionStatus;
    }

    public void setProtectionStatus(String protectionStatus) {
        this.protectionStatus = protectionStatus;
    }

    public String getNtcTemperature() {
        return ntcTemperature;
    }

    public void setNtcTemperature(String ntcTemperature) {
        this.ntcTemperature = ntcTemperature;
    }

    public Integer getBatteryVolume() {
        return batteryVolume;
    }

    public void setBatteryVolume(Integer batteryVolume) {
        this.batteryVolume = batteryVolume;
    }

    public Double getLng() {
        return lng;
    }

    public void setLng(Double lng) {
        this.lng = lng;
    }

    public Double getLat() {
        return lat;
    }

    public void setLat(Double lat) {
        this.lat = lat;
    }

    public Double getSpeed() {
        return speed;
    }

    public void setSpeed(Double speed) {
        this.speed = speed;
    }

    public Double getElectricity() {
        return electricity;
    }

    public void setElectricity(Double electricity) {
        this.electricity = electricity;
    }

    public Integer getDistance() {
        return distance;
    }

    public void setDistance(Integer distance) {
        this.distance = distance;
    }

    public Integer getDumpEnergy() {
        return dumpEnergy;
    }

    public void setDumpEnergy(Integer dumpEnergy) {
        this.dumpEnergy = dumpEnergy;
    }

    public Integer getReportInterval() {
        return reportInterval;
    }

    public void setReportInterval(Integer reportInterval) {
        this.reportInterval = reportInterval;
    }

    @JsonSerialize(using = DateTimeSerializer.class)
    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    /**
     * 状态转换
     * @Param []
     * @Return java.lang.String
     * @Author zxj 2018/7/23
     */
    public String getProtectionStatusName() {
        String name = "";
        if (null != protectionStatus) {
            ProtectionStatus[] names = ProtectionStatus.values();
            char[] str = new StringBuilder(String.format("%0" + names.length + "d", Long.parseLong(Integer.toBinaryString(Integer.parseInt(protectionStatus))))).reverse().toString().toCharArray();

            for (int i = 0; i < str.length; i++) {
                if ('1' == str[i]) {
                    if (StringUtils.isNotEmpty(name)) {
                        name += ",";
                    }
                    name += names[i].name;
                }
            }
        }
        return name;
    }



}
